﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class AccountLogin : System.Web.UI.Page
{
    ACCOUNTSDataAPI acc_api = new ACCOUNTSDataAPI();
    private string email = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblSecurity.Visible = false;
            txtSecurityCode.Visible = false;
            lblMessage.Visible = false;
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string password = txtPassword.Text.Trim();
        string emails = txtEmail.Text.Trim();
        if (txtSecurityCode.Visible == true)
        {
            string securityCode=txtSecurityCode.Text.Trim();
            if (acc_api.AccountCheckLoginBySecurityCode(emails, password, securityCode))
            {
                Session["ACC_EMAIL"] = emails;
                DataTable dt = acc_api.getAccountByEmail(email);
                if (dt != null)
                    Session["ACC_ID"] = dt.Rows[0]["ACC_ID"].ToString();
                Response.Redirect("Appointment.aspx");

            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "The Email or Password or SecurityCode is incorrect!";
            }
        }
        else
        {
            if (acc_api.AccountLogin(emails, password))
            {
                Session["ACC_EMAIL"] = emails;
                DataTable dt = acc_api.getAccountByEmail(emails);
                if (dt != null)
                    Session["ACC_ID"] = dt.Rows[0]["ACC_ID"].ToString();
                Response.Redirect("Appointment.aspx");
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "The Email or Password is incorrect!";
            }
        }
    }
    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        email = txtEmail.Text.Trim();
        if (acc_api.Acc_EmailCheck(email))
        {
            lblSecurity.Visible = true;
            txtSecurityCode.Visible = true;
            return;
        }
        else
        {
            lblSecurity.Visible = false;
            txtSecurityCode.Visible = false;
        }
    }

    protected void chkMark_CheckedChanged(object sender, EventArgs e)
    {
        string pass = txtPassword.Text;
        if (chkMark.Checked == true)
            txtPassword.TextMode = TextBoxMode.SingleLine;
        else
        {
            txtPassword.TextMode = TextBoxMode.Password;
            txtPassword.Text = pass;
        }
    }
}
